/*
 * EnigmaGM - A feature rich guild management system.
 * Copyright (C) 2007 ajiaojr.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */
package org.ajiaojr.enigmagm.client.dialog;

import org.ajiaojr.enigmagm.client.Paged;
import org.ajiaojr.enigmagm.client.data.Event;
import org.ajiaojr.enigmagm.client.data.Raid;
import org.ajiaojr.enigmagm.client.service.EventService;
import org.ajiaojr.enigmagm.client.service.EventServiceAsync;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SourcesTabEvents;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TabListener;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.TableListener;
import com.google.gwt.user.client.ui.VerticalPanel;

import java.util.List;

public class RaidDetailsDialog extends DialogBox {
  public class EventsGrid extends Paged {
    public static final int MAX_RESULTS = 10;

    public EventsGrid(Panel parent) {
      super(parent, MAX_RESULTS);
    }

    public void populate() {
      EventServiceAsync eventService = EventService.Util.getInstance();

      eventService.findByRaidID(raid.getRaidID(), firstResult, maxResults,
          new AsyncCallback() {

            public void onFailure(Throwable caught) {
              Window.alert(caught.getMessage());
            }

            public void onSuccess(Object result) {
              panel.clear();

              final List events = (List) result;

              Grid grid = new Grid(events.size() + 1, 3);

              grid.setWidth("100%");
              grid.setStyleName("gwt-table");
              grid.addStyleName("dataTable");
              grid.setText(0, 0, "Event Name");
              grid.setText(0, 1, "Event Time");
              grid.setText(0, 2, "Comments");
              grid.getRowFormatter().addStyleName(0, "header");
              grid.getColumnFormatter().setWidth(0, "150px");
              grid.getColumnFormatter().setWidth(1, "120px");
              grid.addTableListener(new TableListener() {

                public void onCellClicked(SourcesTableEvents sender, int row,
                    int cell) {
                  if (row < 1)
                    return;
                  Event e = (Event) events.get(row - 1);
                  EventDetailsDialog edd = new EventDetailsDialog(e);
                  hide();
                  edd.show();
                }
              });

              for (int i = 0; i < events.size(); i++) {
                Event e = (Event) events.get(i);
                int currentRow = i + 1;

                grid.setText(currentRow, 0, e.getRaidBossName());
                grid.setText(currentRow, 1, dateTimeFormat.format(e
                    .getEventTimeAsDate()));
                grid.setText(currentRow, 2, e.getComments());
                grid.getRowFormatter().addStyleName(currentRow, "clickable");
              }
              panel.add(grid);
              attachNavBar();
            }
          });
    }

  }

  private DateTimeFormat dateTimeFormat =
      DateTimeFormat.getFormat("dd MMM yyyy HH:mm");

  private EventsGrid eg;

  private Raid raid;

  public RaidDetailsDialog(Raid raid) {
    super(true);
    this.raid = raid;
    init();
  }

  public Raid getRaid() {
    return raid;
  }

  public void setRaid(Raid raid) {
    this.raid = raid;
  }

  private void init() {
    setHTML("Raid Details");
    setTitle("Raid Details");

    final VerticalPanel verticalPanel = new VerticalPanel();
    setWidget(verticalPanel);
    verticalPanel.setSize("500px", "100%");

    final TabPanel tabPanel = new TabPanel();
    verticalPanel.add(tabPanel);
    tabPanel.addTabListener(new TabListener() {
      public boolean onBeforeTabSelected(SourcesTabEvents sender, int tabIndex) {
        return true;
      }

      public void onTabSelected(SourcesTabEvents sender, int tabIndex) {
        switch (tabIndex) {
          case 0:
            eg.populate();
            break;
        }
      }
    });
    tabPanel.setWidth("100%");

    final VerticalPanel eventsPanel = new VerticalPanel();
    tabPanel.add(eventsPanel, "Events");
    eventsPanel.setStyleName("gwt-TabBody");
    eventsPanel.setSpacing(5);

    final VerticalPanel verticalPanel_1 = new VerticalPanel();
    eventsPanel.add(verticalPanel_1);
    verticalPanel_1.setWidth("100%");

    eg = new EventsGrid(verticalPanel_1);

    final VerticalPanel raidPlannerPanel = new VerticalPanel();
    tabPanel.add(raidPlannerPanel, "Planner");
    raidPlannerPanel.setStyleName("gwt-TabBody");
    raidPlannerPanel.setSpacing(5);

    final VerticalPanel verticalPanel_2 = new VerticalPanel();
    raidPlannerPanel.add(verticalPanel_2);

    final Label label = new Label("Under Construction");
    raidPlannerPanel.add(label);
    label.setStyleName("tip");

    final Label hintLabel =
        new Label("Hint: clicking outside this dialog will close it.");
    verticalPanel.add(hintLabel);
    hintLabel.setStyleName("tip");
    tabPanel.selectTab(0);

    setPopupPosition((Window.getClientWidth() - 500) / 2, (Window
        .getClientHeight() - 300) / 2);
  }
}
